package mrdemo011;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class EmpReduce extends Reducer<Text, EmpData, Text, Text>{
	
	Text k3 = new Text("");
	
	EmpData empData = new EmpData();
	
	@Override
	protected void reduce(Text k2, Iterable<EmpData> v2s,
			Reducer<Text, EmpData, Text, Text>.Context context)
			throws IOException, InterruptedException {
		 
		 
		 List<EmpData> list = new ArrayList<EmpData>();
		 
		 for(EmpData v : v2s) {
			 if(v.getFlag() == 0) {
				 //员工
				 list.add(v);
			 }else {
				 //部门
				empData.setDeptno(v.getDeptno());
				empData.setDeptname(v.getDeptname());
			 }
		 }
		 
		 if(empData != null) {
			 for(EmpData item : list) {
				 item.setDeptname(empData.getDeptname());
				 item.setDeptno(empData.getDeptno());
				 context.write(k3, new Text(item.toString()));
			 }
		 }
	 }
	 
}
